from __future__ import annotations

from datetime import date
from typing import List

from quantcore.db import Mongo


class StrongStockService:
    """按 FR-DM-005 计算并落库每日强势股。

    new_high_250：当日创250日新高的股票列表
    limit_up_list：当日涨停的股票列表
    （当前实现依赖日线集合中的占位字段，后续替换为真实逻辑）
    """

    def compute_and_save(self, trade_date: date) -> None:
        col_daily = Mongo.col("price_daily")
        col_strong = Mongo.col("strong_stock_daily")

        # 查询当日文档并根据占位字段识别新高与涨停
        # 注意：目前为简化占位实现，未来应计算250日最高价与涨停逻辑
        cursor = col_daily.find({"_id.trade_date": trade_date})
        new_high_250: List[str] = []
        limit_up_list: List[str] = []
        for doc in cursor:
            symbol = doc["_id"]["symbol"]
            if doc.get("is_new_high_250"):
                new_high_250.append(symbol)
            if doc.get("limit_status", 0) == 1:
                limit_up_list.append(symbol)

        col_strong.replace_one(
            {"_id": trade_date},
            {"_id": trade_date, "new_high_250": new_high_250, "limit_up_list": limit_up_list},
            upsert=True,
        )




